package edu.ucsd.cse135.gas.action.reviewer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import edu.ucsd.cse135.gas.bean.reviewer.ReviewBean;
import edu.ucsd.cse135.gas.logic.applicant.Applicant;
import edu.ucsd.cse135.gas.logic.reviewer.ReviewerFunctions;

public class GradeApplicationAction extends Action {

	public GradeApplicationAction() {
		super();
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		ReviewBean reviewBean = (ReviewBean)form;
		Applicant applicant = (Applicant)request.getSession().getAttribute("applicant");
		
		reviewBean.setReviewer(request.getRemoteUser());
		
		request.getSession().setAttribute("grading", reviewBean);
		
		if(ReviewerFunctions.gradeApplication(reviewBean.getGrade(), reviewBean.getComment(), applicant.getMail()) >= 3)
		{
			return mapping.findForward("complete");
		}
		else
		{
			request.setAttribute("reviewers", ReviewerFunctions.chooseReviewer(request.getRemoteUser()));
			
			return mapping.findForward("choose_reviewer");
		}
		
	}
	
}
